package lession

import "fmt"

/* Arrays 在 GoLang 中是值类型，不是引用类型 */

func ArrayTest() {
	// declaration one
	var a [3]int
	fmt.Println(a)

	// declararion two
	var b [3]int
	b[0] = 12
	b[1] = 33
	b[2] = 44
	fmt.Println(b)

	// declaration three
	var c = [3]int{12, 44, 50}
	fmt.Println(c)

	// declaration four
	var d = [4]int{13}
	fmt.Println(d)

	// declaration five
	e := [...]int{14, 33, 55, 66}
	fmt.Println(e)

	/* Array 在 GO 中是值类型，不是引用类型，值之间的修改不会相互影响 */
	f := [...]string{"USA", "China", "India", "Germany"}
	g := f // 值的复制，不是指针
	g[0] = "Singapore"
	fmt.Println("f is", f)
	fmt.Println("g is", g)

	// Array 作为参数
	num := [...]int{3, 4, 6, 7, 8}
	fmt.Println("before passing to function", num)
	changeLocal(num)
	fmt.Println("after passing to function", num)

	// Array 长度获取
	h := [...]float64{66.7, 33.4, 21, 34}
	fmt.Println("length of h is", len(h))

	// 结合 for...
	k := [...]float64{44.3, 11.3, 53, 66, 66}
	for i := 0; i < len(k); i++ {
		fmt.Printf("%d th element of a is %.2f\n", i, k[i])
		// fmt.Printf("%d th element of a is %.3f\n", i, k[i])
		// fmt.Printf("%d th element of a is %.4f\n", i, k[i])
	}

	// range iterate over an array
	m := [...]float64{332.33, 54.5, 56.444, 323}
	sum := float64(0)
	for index, value := range m {
		fmt.Printf("%d the element of a is %.3f\n", index, value)
		sum += value
	}
	fmt.Println("\n num of all elements of a", sum)

	// _v replace index or value
	for _, value := range m {
		fmt.Printf("value is %.2f\n", value)
	}
}

func changeLocal(num [5]int) {
	num[0] = 55
	fmt.Println("inside function", num)
}
